﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using VideoStore.Common.Enum;
using VideoStore.Domain.Entities;
using VideoStore.Services.Dto;
using VideoStore.Services.Translation.Base;

namespace VideoStore.Services.Translation
{
    public class OrderTranslator : BaseTranslator
    {
        #region Overrides of BaseTranslator

        /// <summary>
        /// Translates to concrete entity.
        /// </summary>
        /// <typeparam name="TDto">The type of the dto.</typeparam>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="dto">The dto.</param>
        /// <param name="concreteEntity">The concrete entity.</param>
        /// <returns></returns>
        public override TEntity TranslateToConcreteEntity<TDto, TEntity>(TDto dto, TEntity concreteEntity)
        {
            OrderDto tempDto = dto as OrderDto;
            if (tempDto != null)
            {
                Order tempEntity = concreteEntity as Order;
                if (tempEntity != null)
                {
                    tempEntity.id = tempDto.Id;
                    tempEntity.total = tempDto.Total;
                    tempEntity.order_date = tempDto.OrderDate;
                    tempEntity.status = (int)tempDto.OrderStatus;
                    tempEntity.customer_id = tempDto.CustomerId;
                    tempEntity.warehouse_id = tempDto.WarehouseId;
                    tempEntity.store_id = tempDto.StoreId;
                    if (tempDto.OrderLinesDto.Count > 0)
                    {
                        foreach (var orderLineDto in tempDto.OrderLinesDto)
                        {
                            Orderline orderLine = new Orderline();
                            orderLine.id = orderLineDto.Id;
                            orderLine.order_id = orderLineDto.OrderId;
                            orderLine.quantity = orderLineDto.Quantity;
                            orderLine.catalogue_id = orderLineDto.CatalogueId;
                            tempEntity.Orderlines.Add(orderLine);
                        }
                    }
                    return tempEntity as TEntity;
                }
            }
            return default(TEntity);
        }

        /// <summary>
        /// Translates to concrete dto.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <typeparam name="TDto">The type of the dto.</typeparam>
        /// <param name="entity">The entity.</param>
        /// <param name="concreteDto">The concrete dto.</param>
        /// <returns></returns>
        public override TDto TranslateToConcreteDto<TEntity, TDto>(TEntity entity, TDto concreteDto)
        {
            Order tempEntity = entity as Order;
            if (tempEntity != null)
            {
                OrderDto tempDto = concreteDto as OrderDto;
                if (tempDto != null)
                {
                    tempDto.Id = tempEntity.id;
                    tempDto.Total = (float)tempEntity.total;
                    tempDto.OrderDate = tempEntity.order_date;
                    tempDto.OrderStatus = (OrderStatus)tempEntity.status;
                    tempDto.CustomerId = tempEntity.customer_id;
                    tempDto.WarehouseId = tempEntity.warehouse_id;
                    tempDto.StoreId = tempEntity.store_id;
                    if (tempEntity.Orderlines.Count > 0)
                    {
                        tempDto.OrderLinesDto = new List<OrderLinesDto>();
                        foreach (var orderLine in tempEntity.Orderlines)
                        {
                            OrderLinesDto orderLineDto = new OrderLinesDto();
                            orderLineDto.Id = orderLine.id;
                            orderLineDto.OrderId = orderLine.order_id;
                            orderLineDto.Quantity = orderLine.quantity;
                            orderLineDto.CatalogueId = orderLine.catalogue_id;
                            tempDto.OrderLinesDto.Add(orderLineDto);
                        }
                    }
                    return tempDto as TDto;
                }
            }
            return default(TDto);
        }

        #endregion
    }
}